# ----------------------------------------------------------------------------
# pytform
# Copyright (c) 2008, Michael Henrique Siegwarth
# All rights reserved.
# 
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions 
# are met:
#
#  * Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#  * Redistributions in binary form must reproduce the above copyright 
#    notice, this list of conditions and the following disclaimer in
#    the documentation and/or other materials provided with the
#    distribution.
#  * Neither the name of pytform nor the names of its
#    contributors may be used to endorse or promote products
#    derived from this software without specific prior written
#    permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
# ----------------------------------------------------------------------------

from math import degrees
import random

import Box2D2 as box2d
import pyglet
from pyglet.gl import *

from base.game_object import *

class Quad(GameObject):
    def __init__(self, x, y, angle, dim_x, dim_y, world):
        super(Quad, self).__init__(x, y, angle, False, world)

        shape_def = box2d.b2PolygonDef()
        shape_def.SetAsBox(dim_x / 2, dim_y /2)
        shape_def.density = 1
        shape_def.friction = 0.3
        shape_def.restitution = 0.3
        
        self.body.CreateShape(shape_def).getAsType()
        self.body.SetMassFromShapes()
        

class BigQuad(Quad):
    def __init__(self, x, y, angle, world, batch):
        super(BigQuad, self).__init__(x, y, angle, 1.0, 1.0, world)

        # Create vertex list
        self.vertex_list.append(batch.add(4, pyglet.gl.GL_QUADS, None,
            'v2f',
            ('c4f', (0, 0.5, 0, 0.5,
                     0.5, 0, 0.5, 0.5,
                     0, 0, 0.5, 0.5,
                     0, 0, 0, 0.5))
        ))

        self.update_vertices()

        self.jump_counter = 0

    def step(self):
        if self.jump_counter == 100:
            self.body.ApplyImpulse(box2d.b2Vec2(random.randint(-3, 3), 5),
                                   box2d.b2Vec2(self.body.GetPosition().x, 
                                                self.body.GetPosition().y))
            self.jump_counter = 0
    
        self.jump_counter += 1

class SmallQuad(Quad):
    def __init__(self, x, y, angle, world, batch):
        super(SmallQuad, self).__init__(x, y, angle, 0.5, 0.5, world)

        # Create vertex list
        self.vertex_list.append(batch.add(4, pyglet.gl.GL_QUADS, None,
            'v2f',
            ('c3f', (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0))
        ))

        self.update_vertices()

        self.events.append(["quad.BigQuad", self.scream])

    def scream(self, trigger_object):
        print "SmallQuad says: a BigQuad has hit me!"


class EventArea(GameObject):
    def __init__(self, x, y, angle, dim_x, dim_y, world, batch):
        super(EventArea, self).__init__(x, y, angle, False, world)
        self.hw = dim_x / 2
        self.hh = dim_y / 2

        shape_def = box2d.b2PolygonDef()
        shape_def.SetAsBox(self.hw, self.hh)
        shape_def.isSensor = True
        
        self.body.CreateShape(shape_def).getAsType()

        # Create vertex list
        if batch != None:
            self.vertex_list.append(batch.add(4, pyglet.gl.GL_QUADS, None,
                'v2f/static',
                ('c4f/static', (0.5, 0.5, 0.5, 0.5,
                         0.5, 0.5, 0.5, 0.5,
                         0.5, 0.5, 0.5, 0.5,
                         0.5, 0.5, 0.5, 0.5 ))
            ))

            self.update_vertices()
        else:
            # TODO - Make this nicer
            self.update_vertices = self.upd

    def upd(self):
        pass

    def scale(self, factor):
        if self.body != None:
            new_hw = self.hw * factor
            new_hh = self.hh * factor

            self.hw = new_hw
            self.hh = new_hh

            self.body.DestroyShape(self.body.GetShapeList())

            shape_def = box2d.b2PolygonDef()
            shape_def.SetAsBox(self.hw, self.hh)
            shape_def.density = 1
            shape_def.friction = 0.3
            shape_def.restitution = 0.3
            
            self.body.CreateShape(shape_def).getAsType()
            self.body.SetMassFromShapes()

            self.update_vertices()

                        
